const api = require("../../../utils/cloudAPIUtils")
const app = getApp();

Page({
    data: {
        submit:{
            nickname:'', //微信昵称
            faceImg:'',  //微信头像
            name:'', //用户昵称
            phone:'',//用户手机号
            picCode:'', //图片验证码
            phoneValid:'', //短信验证码
        },
        showFaceImg:'',
        isSend:true,
        second:60,
        timer:"", //60秒计时器
        time:null,//验证码时间
        VFcode:"",
    },
    btnInput(e){
        const key = e.currentTarget.dataset.key;
        const val = e.detail.value;
        const submit = this.data.submit;
        submit[key] = val;
        this.setData({submit})
    },
     //转base64
     _toBase64OFPng(url){
        return new Promise((resolve)=>{
            wx.getFileSystemManager().readFile({
                filePath:url,
                encoding:'base64',
                success(res){
                    resolve('data:image/jpeg;base64,'+res.data);
                },
                fail(err){
                    console.log(err.errMsg)
                }
            })
        })
    },
    //选择头像
    onChooseAvatar(e){
        const { avatarUrl } = e.detail;
        console.log(avatarUrl)
        this.setData({showFaceImg:avatarUrl})
        const self = this;
        self._toBase64OFPng(avatarUrl).then(base64=>{
            api.post(api.url.userImgUp,{
                content:base64
            },res=>{
                console.log(res);
                if(res.data.code==200){
                    const submit = self.data.submit;
                    submit.faceImg = res.data.body.imgPath;
                    self.setData({submit})
                }else{
                    wx.showToast({title: '上传头像失败',icon:'error'})
                }
            })
        })
    },
    //发送手机验证码
    btnGetNote(){
        const {submit:{phone,picCode},time} = this.data;
        if(!picCode){
            wx.showToast({title: '请输入图片验证码',icon:'error'})
            return;
        }
        if(!this.data.isSend)return;
        this.setData({isSend:false})
        const self = this;
        let second = this.data.second;
        let timer = setInterval(()=>{
            if(second==0){
                clearInterval(timer)
                self.setData({
                    isSend:true,
                    second:60
                })
                return
            }
            self.setData({second:second--})
        },1000)
        api.post(api.url.commonSMS,{
            phone:phone,
            code:picCode,
            pt:'JD'+time,
            t:time
        },res=>{
            if(res.data.code != 200){
                wx.showToast({
                    title: res.data.msg||'发送短信失败！',
                    icon:'none',
                })
                self.setData({isSend:true})
            }
        })
    },
    btnSubmitBind(){
        const {submit,time} = this.data;
        if(!submit.nickname || !submit.name){
            wx.showToast({title: '请输入昵称信息',icon:'error'})
            return;
        }
        if(!submit.faceImg){
            wx.showToast({title: '请上传头像',icon:'error'})
            return;
        }
        if(!submit.phoneValid){
            wx.showToast({title: '输入短信验证码',icon:'error'})
            return;
        }
        wx.showLoading({
          title: '提交中...',
          mask:true
        })
        wx.login({
            success: (res) => {
                api.post(api.url.agentBind,{
                    appId:wx.getAccountInfoSync().miniProgram.appId,
                    wxCode:res.code,
                    nickname:submit.nickname,
                    faceImg:submit.faceImg,
                    name:submit.name,
                    phone:submit.phone,
                    phoneValid:submit.phoneValid,
                    pt:'JD'+time
                },res=>{
                    console.log(res)
                    wx.hideLoading();
                    if(res.data.code==200){
                        wx.showModal({
                            title: '提交绑定成功',
                            content: '退出小程序，并返回代理端查看。',
                            showCancel:false,
                            success(){
                                wx.exitMiniProgram();
                            }
                        })
                    }else{
                        wx.showModal({
                          title: '提交绑定失败',
                          content: res.data.msg||'稍后重新尝试',
                          showCancel:false
                        })
                    }
                })
            },
        })
    },
    onLoad(options) {
        if(!options || !options.phone){
            wx.showModal({
              title: '温馨提示',
              content: '参数错误',
              showCancel:false
            })
        }else{
            const submit = this.data.submit;
            submit.phone = options.phone;
            this.setData({submit})
        }
        this.btnImgCaptcha();
    },
    btnImgCaptcha(){
        const time = Math.round(new Date());
        this.setData({
            time:time,
            VFcode:`${api.getVFcode}?t=${time}`
        })
    },
    onShow(){
        wx.hideHomeButton();
    }
})